package ui;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.wb.swt.SWTResourceManager;
  
public class ShowImages {  
  
    protected Shell shell;  
    public static Display myDisplay;  
    public static boolean internalCall = false;  
  
    /** 
     * Launch the application. 
     * @param args 
     */  
    public static void main(String[] args) {  
        internalCall = true;  
        myDisplay = new Display();  
        try {  
            ShowImages window = new ShowImages();  
            window.open();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    /** 
     * Open the window. 
     */  
    public void open() {  
        Display display = Display.getDefault();  
        createContents(myDisplay);  
        URL url;
		try {
			url = new URL("http://image.sinajs.cn/newchart/daily/n/sh601006.gif");
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			DataInputStream in = new DataInputStream(connection.getInputStream());
			Image img = new Image(display, in);  
			shell.open();  
			GC gc = new GC(shell);  
			gc.drawImage(img, 0, 0);  
			shell.layout();  
			while (!shell.isDisposed()) {  
				if (!display.readAndDispatch()) {  
					display.sleep();  
				}  
			}  
			img.dispose();  
			if (internalCall) display.dispose();  
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }  
  
    /** 
     * Create contents of the window. 
     */  
    protected void createContents(Display display) {  
        myDisplay = display;  
        shell = new Shell(myDisplay,SWT.NO_TRIM);  
//        shell = new Shell(myDisplay,SWT.CLOSE);  
        shell.setBackground(SWTResourceManager.getColor(255, 255, 255));
        shell.addFocusListener(new FocusAdapter() {
        	@Override
        	public void focusLost(FocusEvent e) {
        		shell.setVisible(false);
        	}
        });
        shell.setSize(580, 310);  
        
    }  
}